#include "clientfriendlist.h"
#include "client.h"
#include "sqlconnection.h"
#include "clientmanager.h"

ClientFriendlist::ClientFriendlist(SQLConnection *nSQLConnection, ClientManager *clientManager, Client *owner, QObject *parent) : QObject(parent)
{
    m_TunnelDatabase = nSQLConnection;
    m_clientManager  = clientManager;
    m_owner          = owner;
}

ClientFriendlist::~ClientFriendlist()
{
}

Client* ClientFriendlist::operator[](int i)
{
    if( m_friendlist.size() >= i )
        return NULL;

    return m_friendlist[i].client;
}

void ClientFriendlist::addFriend(const quint32 &uid)
{
    ClientData cData;

    m_TunnelDatabase->fillClientData(&cData, uid);

    m_friendlist.resize(m_friendlist.size() + 1);
    m_friendlist[m_friendlist.size() - 1].client    = m_clientManager->getClient(uid);
    m_friendlist[m_friendlist.size() - 1].uid       = cData.uid;
    m_friendlist[m_friendlist.size() - 1].loginName = cData.loginName;
    m_friendlist[m_friendlist.size() - 1].nickname  = cData.nickName;
    m_friendlist[m_friendlist.size() - 1].online    = ( m_friendlist[m_friendlist.size() - 1].client ? true : false );

    // If the client is currently loggedin, we send him his friendlist again.
    if( m_friendlist[m_friendlist.size() - 1].online )
    {
        m_friendlist[m_friendlist.size() - 1].client->sendFriendlist();

        // Connecting events between those two clients but first disconnect all Signals and slots between those two objects.
        disconnect(m_friendlist[m_friendlist.size() - 1].client, 0, this, 0);
        disconnect(this, 0, m_friendlist[m_friendlist.size() - 1].client, 0);
        connect( m_friendlist[m_friendlist.size() - 1].client, SIGNAL(signal_goOffline(QString)), this, SLOT(on_friendGoesOffline(QString)) );
        connect( this, SIGNAL(signal_goOffline(QString)), m_friendlist[m_friendlist.size() - 1].client, SLOT(on_friendGoesOffline(QString)) );

        // Send online notification to friend.
        m_friendlist[m_friendlist.size() - 1].client->sendNotification_friendComesOnline(m_owner->getName());
    }
}

void ClientFriendlist::initFriendlist()
{
    // Get Clients friendlist from database
    std::vector<ClientFriend> myFriends(0);
    m_TunnelDatabase->getFriendlist(&myFriends, m_owner->getId());

    // Convert all ClientFriend objects into an string and append it to the arguments part of the package...
    foreach( ClientFriend myFriend, myFriends )
    {
        m_friendlist.resize(m_friendlist.size() + 1);
        m_friendlist[m_friendlist.size() - 1].client    = m_clientManager->getClient(myFriend.uid);
        m_friendlist[m_friendlist.size() - 1].uid       = myFriend.uid;
        m_friendlist[m_friendlist.size() - 1].loginName = myFriend.loginName;
        m_friendlist[m_friendlist.size() - 1].nickname  = myFriend.nickName;
        m_friendlist[m_friendlist.size() - 1].online    = myFriend.isOnline;

        if( myFriend.isOnline )
        {
            // Connecting events between those two clients but first disconnect all Signals and slots between those two objects.
            disconnect(m_friendlist[m_friendlist.size() - 1].client, 0, this, 0);
            disconnect(this, 0, m_friendlist[m_friendlist.size() - 1].client, 0);
            connect( m_friendlist[m_friendlist.size() - 1].client, SIGNAL(signal_goOffline(QString)), this, SLOT(on_friendGoesOffline(QString)) );
            connect( this, SIGNAL(signal_goOffline(QString)), m_friendlist[m_friendlist.size() - 1].client, SLOT(on_friendGoesOffline(QString)) );

            // Send online notification to friend.
            m_friendlist[m_friendlist.size() - 1].client->sendNotification_friendComesOnline(m_owner->getName());
        }
    }
}

QString ClientFriendlist::toString()
{
    QString result = ""; // String in wich we store the friendlist.

    // Convert all ClientFriend objects into an string and append it to the arguments part of the package...
    for( unsigned int i = 0; i < m_friendlist.size(); ++i )
    {
        // name:nickName:online/offline;name2:nickname2:online/offline;...
        result += m_friendlist[i].loginName + ":";
        result += m_friendlist[i].nickname + ":"; // If the client has no nickname take the loginname.
        result += ( m_friendlist[i].online ? "online" : "offline" );
        result += ";";
    }

    return result;
}
